<template>
  <div class="recommend-app">
    <header class="header-bar">
      <h2 class="title">推荐应用</h2>
    </header>
    <div class="content">
      <tab-bar class="tabbar" :tabs="appTypes" titleKey="text" @change="tabChange" />
      <div class="website-list">
        <el-row :gutter="20">
          <el-col v-for="item in websites" :key="item.id" :span="8" class="app-item">
            <dl href="javascript:;" @click="addApp(item)">
              <dt class="app-item-icon">
                <img :src="item.icon" class="app-item-img" />
              </dt>
              <dd>
                <h2 class="app-title">{{item.name}}</h2>
                <p class="app-des">{{item.describe}}</p>
                <el-button class="add-btn" type="primary" icon="el-icon-plus" size="mini" circle :style="{
                  padding: '3px'
                }"></el-button>
              </dd>
            </dl>
          </el-col>
        </el-row>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'recommend-app',
  data () {
    return {
      appTypes: [],
      websites: [],
      fetchWebsitesParams: {
        page: 1,
        page_size: 100,
        type_id: ''
      }
    }
  },
  watch: {
    fetchWebsitesParams: {
      handler: 'fetchWebsiteList',
      deep: true
    }
  },
  created () {
    this.fetchAppTypes()
  },
  methods: {
    async fetchWebsiteList () {
      const res = await this.$api.app.list(this.fetchWebsitesParams)
      this.websites = res.data
    },
    async fetchAppTypes () {
      const res = await this.$api.app.type()
      this.appTypes = res.data
      this.fetchWebsitesParams.type_id = res.data[0].id
    },
    tabChange (index) {
      this.fetchWebsitesParams.type_id = this.appTypes[index].id
    },
    addApp (item) {
      const apps = this.$store.state.apps
      apps.push({
        icon: item.icon,
        title: item.name,
        id: item.id,
        des: item.des,
        url: item.url
      })
      this.$store.commit('SET_APPS', [...apps])
    }
  }
}
</script>

<style lang="scss" scoped>
.recommend-app {
  padding: 20px 20px 0 20px;
  .header-bar {
    border-bottom: 1px solid #eee;
    margin-bottom: 20px;
    .title {
      font-size: 28px;
      line-height: 60px;
    }
  }
  .content {
    /deep/.tabbar {
      a {
        background: #eee;
        padding: 5px 10px;
        border-radius: 10px;
        border: 0;
        margin-right: 10px;
        margin-bottom: 10px;
        &.active {
          background: $color-primary;
          color: #fff;
        }
      }
    }
    .website-list {
      height: 420px;
      overflow: auto;
      .app-item {
        margin-top: 20px;
        dl {
          @include flex(row, flex-start, center);
          background: #f2f2f2;
          padding: 10px;
          border-radius: 4px;
          dt {
            @include wh(80px, 80px);
            border-radius: 12px;
            overflow: hidden;
            flex-shrink: 0;
            box-shadow: 0 0 5px #0000001a;
            .app-item-img {
              display: block;
              width: 100%;
              height: 100%;
              background-repeat: no-repeat;
              background-size: cover;
              background-color: #fff;
            }
          }
        }
        dd {
          padding-left: 10px;
          .app-title {
            font-size: 14px;
            color: #000;
            margin-bottom: 10px;
          }
          .app-des {
            @include ellipsis(2);
            font-size: 12px;
            color: #666;
          }
          .add-btn {
            float: right;
            margin-top: 10px;
          }
        }
      }
    }
  }
}
</style>
